#include "graphe.h"


Graphe::Graphe() {srand(time(NULL));}

Graphe::Graphe(const Graphe& g) 
{
	int n = g.getN();
	int m = g.getM();
	
	for(int i=0; i<n; i++) 
	{
                sommets.push_back(g.sommets.at(i));
	}
	
	for(int i=0; i<m; i++)
	{	
                aretes.push_back(g.aretes.at(i));
	}
	
}

Graphe::~Graphe() {}

void Graphe::removeSommet(Sommet *s) 
{
	int del = indexOf(s);	
	removeSommet(del);
}

void Graphe::removeSommet(int i) 
{
	Sommet s = *sommets.at(i);
        vector <Arete*> v = getAreteIncidente(&s);
        for (int r=0;r<(int)v.size();r++)
		{
		    removeArete(v.at(r));
		}
	sommets.erase(sommets.begin()+i);
}


void Graphe::removeArete(Arete* a)
{
	int del = indexOf(a);
        removeArete(del);
}

void Graphe::removeArete(int i) 
{
	aretes.erase(aretes.begin()+i);
}

Arete* Graphe::getArete(Sommet *u, Sommet *v)const
{
	int m = getM();

	for(int i=0; i<m; i++) 
	{
                Sommet *u0 = aretes.at(i)->getU();
                Sommet *v0 = aretes.at(i)->getV();
		if((u == u0 && v == v0) || (u == v0 && v == u0)) 
			{return aretes.at(i);}
	}
	cout<<"ERROR l arete n'appartient pas au graphe"<<endl;
        return NULL;
}


vector<Arete*> Graphe::getAreteIncidente(Sommet *u)const
{
       /* int m = getM();
        vector<Arete*> v;

        for(int i=0; i<m; i++)
	{
                Sommet *u0 = aretes.at(i)->getU();
                Sommet *v0 = aretes.at(i)->getV();
		if((u == u0) || (u == v0))
			{v.push_back(aretes.at(i));}
	}
        return v ;*/

       return u->getAreteIncidente();

}

void Graphe::addSommet(Sommet *s) {sommets.push_back(s);}

void Graphe::addSommet(float x, float y) {sommets.push_back(new Sommet(x, y, getN()+1));}

void Graphe::addArete(Arete *a) {aretes.push_back(a);}
void Graphe::addArete(Sommet *u, Sommet *v) 
{
        Arete *a= new Arete(u,v);
	aretes.push_back(a);
}

void Graphe::removeAllAretes() {aretes.clear();}

void Graphe::clear() {aretes.clear(); sommets.clear();}
vector<Sommet*> Graphe::getSommets()const {return sommets;}
Sommet Graphe::getSommet(int i)const {return *sommets[i];}
int Graphe::getN()const {return sommets.size();}
vector<Arete*> Graphe::getAretes()const {return aretes;}
Arete* Graphe::getArete(int i)const {return aretes[i];}

int Graphe::getM()const {return aretes.size();}
void Graphe::generer(){}
void Graphe::generer2(){}
		
void Graphe::affiche()const
{
	cout<<endl;
	cout<< "SOMMET" <<endl<<endl;
	for(int i=0; i<getN(); i++)
	{
		sommets[i]->affiche();
	}
	cout<<endl;
	cout<< "ARETES" <<endl<<endl;
	for(int i=0; i<getM(); i++)
	{
                aretes[i]->affiche();
	}
	cout<<endl;
}

void Graphe::afficheSV()const
{
	cout<<endl;
	cout<< "SOMMETS" <<endl<<endl;
	for(int i=0; i<getN(); i++)
	{
		sommets[i]->afficheSV();
	}
	cout<<endl;
	cout<< "ARETES" <<endl<<endl;
	for(int i=0; i<getM(); i++)
	{
                aretes[i]->affiche();
	}
	cout<<endl;
}

int Graphe::indexOf(Sommet *s)
{
	for(int i=0; i<getN(); i++)
		{
			if(sommets.at(i)==s)	
				{return i;}
		}
	cout<<"ERROR le sommet n'appartient pas au graphe"<<endl;
	return -1;
}

int Graphe::indexOf(Arete *a)
{
	for(int i=0; i<getM(); i++)
		{
			if(aretes[i]==a) 
				{return i;}
		}
	cout<<"ERROR l arete n'appartient pas au graphe"<<endl;
	return -1;
		
}

